package com.cheer.ba.view.activity;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.model.BreakRulesModel;
import com.cheer.ba.model.ResultBreak;
import com.cheer.ba.presenter.impl.CarPresenter;
import com.cheer.ba.utils.CountDownTimer;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.separatelistview.SwipeListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 描述 ：查询车辆列表
 * 作者 ：hq
 * 时间 ：2017/5/18 14:02
 */
public class BreakRuleCarListAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    //没有车辆信息
    @BindView(R.id.id_car_txt)
    TextView mErrorTxt;

    //滑动删除
    @BindView(R.id.id_break_cars_listv)
    SwipeListView mListView;

    private CommonAdapter<BreakRulesModel> mAdapter;
    private List<BreakRulesModel> mRoutes = new ArrayList<BreakRulesModel>();

    @Override
    protected boolean isSupportSwipeback() {
        return false;
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_break_car_list, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CarPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.find_bad_rule));
        TextView mRight = setTitleRight("添加车辆", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showActivity(aty, BreakRulesCarAty.class);
            }
        });
        Drawable drawable = getResources().getDrawable(R.drawable.ic_add_oil_card);
        drawable.setBounds(0, 0, (int) getResources().getDimension(R.dimen.px52), (int) getResources().getDimension(R.dimen.px52));
        mRight.setCompoundDrawables(drawable, null, null, null);
        mAdapter = new CommonAdapter<BreakRulesModel>(aty, mRoutes, R.layout.adp_break_rules) {
            @Override
            public void convert(final ViewHolder helper, final BreakRulesModel item) {
                if (null != item) {
                    helper.setText(R.id.id_car_num_txt, item.mCarLsprefix + " " + item.mCarLsnum);
                    helper.setText(R.id.id_car_desc_txt, item.mCarName + " " + item.mCarVers);
                    if (item.mCarType == 1) {
                        helper.setBackgroundResource(R.id.id_car_img, R.drawable.ic_small_car);
                    } else {
                        helper.setBackgroundResource(R.id.id_car_img, R.drawable.ic_large_car);
                    }
                    LinearLayout mTxt = (LinearLayout) helper.getViewById(R.id.id_del_car_Linear);
                    LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(mListView.getRightViewWidth(),
                            LinearLayout.LayoutParams.MATCH_PARENT);
                    mTxt.setLayoutParams(lp2);
                    mTxt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mListView.hiddenRight(mListView.getChildAt(helper.getPosition()));
                            ((CarPresenter) presenter).delAndSaveCarCache(mRoutes, item);
                        }
                    });
                }
            }
        };
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        List<BreakRulesModel> mCars = (List<BreakRulesModel>) ((CarPresenter) presenter).getCacheData();
        updateViewWithTag(mCars, ConstantTag.DEL_2_SAVE_CAR);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (CountDownTimer.isFastClick()) {
            return;
        }
        BreakRulesModel rules = (BreakRulesModel) mAdapter.getItem(position);
        if (null != rules) {
            ((CarPresenter) presenter).searchCarInfo(rules.mCarLsprefix, rules.mCarLsnum, rules.mCarFrameno);
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(ConstantTag.DEL_2_SAVE_CAR, tag)) {
                List<BreakRulesModel> mCars = (List<BreakRulesModel>) t;
                if (null != mCars && !mCars.isEmpty()) {
                    mErrorTxt.setVisibility(View.GONE);
                    mListView.setVisibility(View.VISIBLE);
                    mRoutes.clear();
                    mRoutes.addAll(mCars);
                    mAdapter.notifyDataSetChanged();
                    return;
                }
            }
        }
        LogUtils.LogD("car", "显示了");
        dataStatus(4);
        mErrorTxt.setVisibility(View.VISIBLE);
        mListView.setVisibility(View.GONE);
    }

    @Override
    public <T> void updateView(T t) {
        if (null != t) {
            ResultBreak rules = (ResultBreak) t;
            if (null != rules) {
                WatchCarInfoAty.startWatchCarInfoAty(aty, rules);
            }
        }
    }
}
